﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using Entity;
using Business; 

namespace WebApp
{
   
    public partial class NewsforAdmin : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Bindnews();
        }
        protected void Add_news(object sender, EventArgs e)
        {
            News_Entity news = new News_Entity();
            News_Bussiness Insert = new News_Bussiness();

            news.Topicname = txtname.Text.Trim();
            news.Topic = txttopic.Text.Trim();


            Insert.Add_News(news);
            Bindnews();
            

            txtname.Text = "";
            txttopic.Text = "";
        }
        protected void ResetAllFields(object sender, EventArgs e)
        {
            txtname.Text = "";
            txttopic.Text = "";
            
        }

        private void Bindnews()
        {

            GridView1.DataSource = GridDataSource();
            GridView1.DataBind();
        }
        private DataTable GridDataSource()
        {
            News_Bussiness p = new News_Bussiness();
            DataTable dt = new DataTable();
            try
            {
                dt = p.Load();
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
            finally
            {
                p = null;
            }

            return dt;
        }

        protected void Editing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bindnews();
        }

        protected void Cancel(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bindnews();
        }

        protected void Update(object sender, GridViewUpdateEventArgs e)
        {
            News_Bussiness _ObjBusiness = new News_Bussiness();
                GridViewRow row = GridView1.Rows[e.RowIndex];
               int newsID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
                int getRowCount = 0;
                //TextBox txtname = (TextBox)row.FindControl("txtname");
                //TextBox txttopic = (TextBox)row.FindControl("txttopic");
                string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
                string topic = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
                getRowCount = _ObjBusiness.Update(newsID, name,topic);
                GridView1.EditIndex = -1;
                Bindnews();

    
        }

        protected void Delete(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);


            // instantiate BAL
            News_Bussiness pBAL = new News_Bussiness();
            try
            {
                pBAL.Delete(id);

                lblm.Text = "Record Deleted Successfully.";
            }
            catch (Exception ee)
            {
                lblm.Text = ee.Message.ToString();
            }
            finally
            {
                pBAL = null;
            }

            GridView1.EditIndex = -1;
            // Refresh the list
            Bindnews();
        }

        protected void Select(object sender, EventArgs e)
        {
            GridView1.SelectedRowStyle.BackColor = System.Drawing.Color.Yellow;
            
            txtname.Text = GridView1.SelectedRow.Cells[1].Text;
            txttopic.Text = GridView1.SelectedRow.Cells[2].Text; 
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            
            
        }
    }
}